define(function(require){
	var main =require('/modules/js/seajs/main.js');
	var m = new main();
	function Cont(){
		this.Fun._init();
	}
	Cont.prototype.Fun = {
		_init:function(){
			this.auto();
		},
		auto:function(){
			table = $('.dataTables-example').DataTable({
    			  "ajax": {
				    "url": "/employee/userinfomng-list",
				    "dataSrc": function ( json ) {
                      for ( var i=0, ien=json.Models.length ; i<ien ; i++ ) {
                        json.Models[i][0] = '<a href="/message/'+json.Models[i][0]+'>View message</a>';
                      }
                      return json.Models;
                    },
				  },
				  "columnDefs": [ 
				  	{
			            "searchable": false,
			            "orderable": false,
			            "targets": 0
			        },
			        { 
		            	"data": "UserId" ,
				  		"targets": 1,
		        	},
			        { 
		            	"data": "Photo" ,
				  		"targets": 2,
		        	},
		            { 
		            	"data": "Nickname" ,
				  		"targets": 3,
		        	},
		        	{ 
		            	"data": "SexName" ,
				  		"targets": 4,
		        	},
		        	{ 
		            	"data": "Phone" ,
				  		"targets": 5,
		        	},
		            { 
		            	"data": "ENTCode" ,
				  		"targets": 6,
		        	},
		        	{ 
		            	"data": "UserLevelName" ,
				  		"targets": 7,
		        	},
		        	{ 
		            	"data": "CustomerCount" ,
				  		"targets": 8,
		        	},
				  	{
			            "data": null,
			            "targets": 9,
			            "defaultContent":'<button class="btn btn-info btn-xs eidtUser" type="button" style="margin-bottom: 0px;" data-toggle="modal" data-target="#AddRole" ><i class="fa fa-pencil-square-o"></i> 编辑</button>\
						   <button class="btn btn-danger btn-xs delUser" type="button" style="margin-bottom: 0px;"><i class="fa fa-times"></i> 删除</button>'
			        } ],
			        "order": [[0, 'asc']]

    		})
		  	table.on('order.dt search.dt',function() {
		        table.column(0, {
		            search: 'applied',
		            order: 'applied'
		        }).nodes().each(function(cell, i) {
		            cell.innerHTML = i + 1;
		        });
		    }).draw();
		    // 添加
		    // type 100 添加  200 编辑
			var btn = m.QS('#commentForm');
			m.addEvent(btn,'submit',function(e){
				var v  = e.target.dataset;
				var type  = v.type;
				var userid  = v.userid;
				var ENTCodeobj = m.QS('#ENTCode');
				var UserLevelNoobj = m.QS('#UserLevelNo');
				var codeIndex = ENTCodeobj.selectedIndex;
				var ENTCode = ENTCodeobj.options[codeIndex].value;
				var leveIndex = UserLevelNoobj.selectedIndex;
				var UserLevelNo = UserLevelNoobj.options[leveIndex].value;
				var Sex = $('#Sex input:radio[name="radioInline"]:checked').val();
				var data = {
					'Nickname': m.QS('#Nickname').value,
					'Phone': m.QS('#Phone').value,
					'UserLevelNo': UserLevelNo,
					'Sex': parseInt(Sex),
					'type':type,
				}
				if(parseInt(type) == 100){
					data.ENTCode = ENTCode;
				}else{
					data.UserId = userid;
				}
				m.post({
					url:'/employee/create-user-info',
					data:data,
					success:function (ret) {
		                if (ret.IsBizSuccess) {
		                	var msg = parseInt(type)  == 100 ? '添加成功':'修改成功';
		                	layer.msg(msg);
							$('#AddRole').modal('hide')
							table.ajax.reload();
		                } else {
							layer.msg(ret.ErrMsg);
							return ;
		                }
		            }
            	})

            	var e = e || window.event;  
    			if (e.preventDefault) e.preventDefault();  
    			else window.event.returnValue = false; 
				return ;
            	
			})
			// 点击编辑按钮
			$('.dataTables-example tbody').on('click', '.eidtUser', function () {
		        var data = table.row($(this).parents('tr')).data();
		        m.QS('.w_titadd span').innerHTML = '编辑'
				m.QS('.ENTCode').style.display = 'none';
				m.QS('.ENTCode select').removeAttribute('required');
		        m.QS('#Nickname').value = data.Nickname;
		        m.QS('#Phone').value = data.Phone;
		        m.QS('#UserLevelNo').value = data.UserLevelNo;
		        m.QS('#commentForm').setAttribute('data-type',200)
		        m.QS('#commentForm').setAttribute('data-userid',data.UserId)
			});
			// 点击添加
			$(document).on('click','.AddRole',function(e){
				m.QS('.w_titadd span').innerHTML = '添加'
				m.QS('#commentForm').setAttribute('data-type',100)
				m.QS('#commentForm').setAttribute('data-userid','')
				m.QS('.ENTCode').style.display = 'block';
				m.QS('.ENTCode select').setAttribute('required',true);
				m.QS('#Nickname').value = '';
				m.QS('#Phone').value = '';
				m.QS('#ENTCode').value = '';
				m.QS('#UserLevelNo').value = '';
			})
			// 删除
			$(document).on('click','.delUser',function(e){
				var data = table.row($(this).parents('tr')).data();
				m.post({
					url:'/employee/del-user-info',
					data:{UserId:data.UserId},
					success:function (ret) {
		                if (ret.IsBizSuccess) {
		                	layer.msg('删除成功');
		                	table.ajax.reload();
		                } else {
							layer.msg(ret.ErrMsg);
							return ;
		                }
		            }
	        	})
			})
		},


	}
	var a = new Cont();


})